﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace SizeboxUI
{
	// Token: 0x020003BE RID: 958
	public class ST_EntryManager : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler, IEventSystemHandler
	{
		// Token: 0x06001A20 RID: 6688 RVA: 0x000A33CF File Offset: 0x000A15CF
		private void Start()
		{
			this.blink_default = null;
			this.blink_shown = true;
		}

		// Token: 0x06001A21 RID: 6689 RVA: 0x000A33E0 File Offset: 0x000A15E0
		public void DeleteObject()
		{
			if (this.obj == null)
			{
				return;
			}
			InterfaceControl control = this.stv.GetComponentInParent<EditView>().control;
			if (control.selectedEntity == this.obj)
			{
				control.SetSelectedObject(null, true);
			}
			this.obj.GetComponent<EntityBase>().DestroyObject(true);
			this.obj = null;
		}

		// Token: 0x06001A22 RID: 6690 RVA: 0x000A3448 File Offset: 0x000A1648
		public void OnPointerClick(PointerEventData eventData)
		{
			base.CancelInvoke();
			this.ResetFlash();
			if (this.category)
			{
				if (eventData.button == PointerEventData.InputButton.Left)
				{
					if (this.categoryExpanded && (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)))
					{
						this.categoryChildren.ForEach(delegate(GameObject obj)
						{
							this.stv.ToggleSelected(obj, false);
						});
						return;
					}
					this.SetCategoryExpanded(!this.categoryExpanded, false);
				}
				else if (eventData.button == PointerEventData.InputButton.Right)
				{
					if (!Input.GetKey(KeyCode.LeftControl) && !Input.GetKey(KeyCode.RightControl))
					{
						this.stv.ClearSelected();
					}
					this.categoryChildren.ForEach(delegate(GameObject obj)
					{
						this.stv.ToggleSelected(obj, false);
					});
					this.stv.OnRightClick();
				}
				return;
			}
			if (eventData.button == PointerEventData.InputButton.Left)
			{
				if (GlobalPreferences.SL_SelObjOnEntryClick.value && (this.stv.GetSelectedEntries().Count == 0 || (this.stv.GetSelectedEntries().Count == 1 && !Input.GetKey(KeyCode.LeftControl) && !Input.GetKey(KeyCode.RightControl))))
				{
					this.stv.GetComponentInParent<EditView>().control.SetSelectedObject(this.obj.GetComponent<EntityBase>(), false);
				}
				this.stv.ToggleSelected(base.gameObject, !Input.GetKey(KeyCode.LeftControl) && !Input.GetKey(KeyCode.RightControl));
			}
			else if (eventData.button == PointerEventData.InputButton.Right)
			{
				if (this.stv.IsEntrySelected(base.gameObject))
				{
					this.stv.OnRightClick();
					return;
				}
				if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
				{
					this.stv.ToggleSelected(base.gameObject, false);
					this.stv.OnRightClick();
					return;
				}
				this.stv.ToggleSelected(base.gameObject, true);
				this.stv.OnRightClick();
			}
		}

		// Token: 0x06001A23 RID: 6691 RVA: 0x000A3668 File Offset: 0x000A1868
		public void OnPointerEnter(PointerEventData eventData)
		{
			if (this.stv.IsEntrySelected(base.gameObject) || this.category)
			{
				return;
			}
			base.gameObject.GetComponent<Image>().color = new Color(0.39f, 0.78f, 1f, 0.66f);
			if (this.obj.GetComponent<CityBuilder>() != null)
			{
				return;
			}
			base.InvokeRepeating("Flash", 0f, 0.4f);
		}

		// Token: 0x06001A24 RID: 6692 RVA: 0x000A36EC File Offset: 0x000A18EC
		public void OnPointerExit(PointerEventData eventData)
		{
			if (this.stv.IsEntrySelected(base.gameObject) || this.category)
			{
				return;
			}
			base.gameObject.GetComponent<Image>().color = new Color(1f, 1f, 1f, 0.39f);
			base.CancelInvoke();
			this.ResetFlash();
		}

		// Token: 0x06001A25 RID: 6693 RVA: 0x000A3750 File Offset: 0x000A1950
		private void ResetFlash()
		{
			if (!this.blink_shown)
			{
				this.Flash();
			}
		}

		// Token: 0x06001A26 RID: 6694 RVA: 0x000A3764 File Offset: 0x000A1964
		private void Flash()
		{
			Renderer[] componentsInChildren = this.obj.GetComponentsInChildren<Renderer>();
			this.blink_shown = !this.blink_shown;
			if (!base.gameObject.activeInHierarchy && this.blink_shown)
			{
				base.CancelInvoke();
			}
			if (this.blink_default == null || this.blink_default.Count < componentsInChildren.Length)
			{
				this.blink_default = new List<bool>();
				for (int i = 0; i < componentsInChildren.Length; i++)
				{
					this.blink_default.Add(componentsInChildren[i].enabled);
				}
			}
			for (int j = 0; j < componentsInChildren.Length; j++)
			{
				componentsInChildren[j].enabled = (this.blink_shown && this.blink_default[j]);
			}
		}

		// Token: 0x06001A27 RID: 6695 RVA: 0x000A3838 File Offset: 0x000A1A38
		public void Refresh()
		{
			if (this.category || this.obj == null || this.obj.GetComponent<EntityBase>() == null)
			{
				return;
			}
			EntityBase component = this.obj.GetComponent<EntityBase>();
			base.GetComponentsInChildren<Image>()[1].sprite = ((!component.isPlayer) ? ((!component.isGiantess) ? ((!component.isMicro) ? this.stv.icon_object : this.stv.icon_micro) : this.stv.icon_giantess) : this.stv.icon_player);
			base.GetComponentInChildren<Text>().text = this.obj.name;
		}

		// Token: 0x06001A28 RID: 6696 RVA: 0x000A3904 File Offset: 0x000A1B04
		public void UpdateCategory()
		{
			if (this.categoryChildren.Count == 0)
			{
				this.stv.categories[this.stv.GetSortScore(this.categoryType)] = null;
				UnityEngine.Object.Destroy(base.gameObject);
			}
			else
			{
				base.gameObject.GetComponentInChildren<Text>().text = this.stv.GetSortName(this.categoryType) + " (" + this.categoryChildren.Count.ToString() + ")";
			}
		}

		// Token: 0x06001A29 RID: 6697 RVA: 0x000A3998 File Offset: 0x000A1B98
		public void CleanupChildren()
		{
			for (int i = 0; i < this.categoryChildren.Count; i++)
			{
				if (!(this.categoryChildren[i].GetComponent<ST_EntryManager>().obj != null))
				{
					UnityEngine.Object.Destroy(this.categoryChildren[i]);
					this.categoryChildren.RemoveAt(i);
					i--;
				}
			}
			this.UpdateCategory();
		}

		// Token: 0x06001A2A RID: 6698 RVA: 0x000A3A10 File Offset: 0x000A1C10
		public ST_EntryManager GetEntryByObject(GameObject go)
		{
			for (int i = 0; i < this.categoryChildren.Count; i++)
			{
				ST_EntryManager component = this.categoryChildren[i].GetComponent<ST_EntryManager>();
				if (component.obj == go)
				{
					return component;
				}
			}
			return null;
		}

		// Token: 0x06001A2B RID: 6699 RVA: 0x000A3A60 File Offset: 0x000A1C60
		public bool HasObject(GameObject go)
		{
			for (int i = 0; i < this.categoryChildren.Count; i++)
			{
				ST_EntryManager component = this.categoryChildren[i].GetComponent<ST_EntryManager>();
				if (component.obj == go)
				{
					return true;
				}
			}
			return false;
		}

		// Token: 0x06001A2C RID: 6700 RVA: 0x000A3AB0 File Offset: 0x000A1CB0
		public void SetCategoryExpanded(bool val, bool cleanup = false)
		{
			this.categoryChildren.ForEach(delegate(GameObject obj)
			{
				ST_EntryManager component = obj.GetComponent<ST_EntryManager>();
				component.gameObject.SetActive(val);
			});
			this.categoryExpanded = val;
			if (!cleanup)
			{
				this.stv.sortShown[this.stv.GetSortScore(this.categoryType)] = val;
			}
		}

		// Token: 0x04001C09 RID: 7177
		public SceneTreeView stv;

		// Token: 0x04001C0A RID: 7178
		public GameObject obj;

		// Token: 0x04001C0B RID: 7179
		public ST_EntryManager.EntityDisplayType type;

		// Token: 0x04001C0C RID: 7180
		public bool category;

		// Token: 0x04001C0D RID: 7181
		public bool categoryExpanded;

		// Token: 0x04001C0E RID: 7182
		public List<GameObject> categoryChildren;

		// Token: 0x04001C0F RID: 7183
		public ST_EntryManager.EntityDisplayType categoryType;

		// Token: 0x04001C10 RID: 7184
		private List<bool> blink_default;

		// Token: 0x04001C11 RID: 7185
		private bool blink_shown;

		// Token: 0x020003BF RID: 959
		public enum EntityDisplayType
		{
			// Token: 0x04001C13 RID: 7187
			Giantess,
			// Token: 0x04001C14 RID: 7188
			Micro,
			// Token: 0x04001C15 RID: 7189
			Object
		}
	}
}
